package com.crm.system.controller.performanceRankingManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.contractManager.RepaymentAbility;
import com.crm.model.entity.finishOrderProduct.PersonalPerformanceAnalysis;
import com.crm.model.qo.performanceRankingManager.PerformanceRankingQO;
import com.crm.model.qo.performanceRankingManager.PerformanceRankingYmQO;
import com.crm.model.vo.performanceRankingManager.*;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.performanceRankingManager.PerformanceRankingDbService;
import com.crm.service.returnMoney.FinishOrderDBservice;
import com.crm.system.service.performanceRankingManager.PerformanceRankingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/8/27 11:02
 */
@RestController
@RequestMapping("/performanceRanking")
@Slf4j
public class PerformanceRankingController {
    @Autowired
    PerformanceRankingService performanceRankingService;
    @Autowired
    PerformanceRankingDbService performanceRankingDbService;
    @Autowired
    FinishOrderDBservice finishOrderDBservice;
    @Autowired
    CustomerContractDbService customerContractDbService;

    /**
     * 个人业绩统计
     *
     * @param performanceRankingQO 参数
     * @return {@link {com.crm.common.model.DataResponse}}
     * @Author pym
     * @Date 2021/8/27 14:02
     */
    @PostMapping("/personalPerformanceRanking")
    public DataResponse personalPerformanceRanking(PerformanceRankingQO performanceRankingQO) {
        try {
            List<PersonalPerformanceRankingVO> personalPerformanceRankingVOList
                    = performanceRankingService.personalPerformanceRanking(performanceRankingQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(personalPerformanceRankingVOList));
        } catch (Exception e) {
            log.error("performanceRanking personalPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 公司业绩统计
     *
     * @param performanceRankingQO 参数
     * @return {@link {com.crm.common.model.DataResponse}}
     * @Author pym
     * @Date 2021/8/27 14:03
     */
    @PostMapping("/companyPerformanceRanking")
    public DataResponse companyPerformanceRanking(PerformanceRankingQO performanceRankingQO) {
        try {
            List<CompanyPerformanceRankingVO> companyPerformanceRankingVOList
                    = performanceRankingService.companyPerformanceRanking(performanceRankingQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(companyPerformanceRankingVOList));
        } catch (Exception e) {
            log.error("performanceRanking companyPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 根据年月公司业绩统计
     */
    @PostMapping("/companyPerformanceRankingByYm")
    public DataResponse companyPerformanceRankingByYm(@RequestBody PerformanceRankingYmQO performanceRankingYmQO) {
        try {
            List<CompanyPerformanceRankingYmVO> companyPerformanceRankingYmVOS = performanceRankingDbService.companyPerformanceRankingByYm(performanceRankingYmQO.getYmDateStr());
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(companyPerformanceRankingYmVOS));
        } catch (Exception e) {
            log.error("performanceRanking companyPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 产品占比分析
     */
    @PostMapping("/getAnalysisOfProductProportion")
    public DataResponse getAnalysisOfProductProportion(@RequestBody PerformanceRankingYmQO performanceRankingYmQO) {
        try {
            List<AnalysisOfProductProportionVO> analysisOfProductProportion = performanceRankingDbService.getAnalysisOfProductProportion(performanceRankingYmQO.getYmDateStr(), performanceRankingYmQO.getAccountId());
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(analysisOfProductProportion));
        } catch (Exception e) {
            log.error("performanceRanking companyPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 业绩分析表格
     */
    @PostMapping("/getPersonalPerformanceAnalysis")
    public DataResponse getNewSingleMemberStatistics(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                     @RequestParam(value = "pageSize", required = true) Integer pageSize,
                                                     @RequestParam(value = "yearMonth", required = true) String yearMonth,
                                                     @RequestParam(value = "accountId", required = true) String accountId) {
        try {
            DhPageInfo<PersonalPerformanceAnalysis> personalPerformanceAnalysis
                    = finishOrderDBservice.getPersonalPerformanceAnalysis(pageIndex, pageSize, yearMonth, accountId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(personalPerformanceAnalysis));
        } catch (Exception e) {
            log.error("performanceRanking companyPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 个人业绩组成
     */
    @PostMapping("/getPerformanceComposition")
    public DataResponse getPerformanceComposition(@RequestBody PerformanceRankingYmQO performanceRankingYmQO) {
        try {
            List<PerformanceComposition> analysisOfProductProportion = performanceRankingDbService.getPerformanceComposition(performanceRankingYmQO.getYmDateStr(), performanceRankingYmQO.getAccountId());
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(analysisOfProductProportion));
        } catch (Exception e) {
            log.error("performanceRanking companyPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }


    /**
     * 回款能力
     */
    @PostMapping("/getRepaymentAbility")
    public DataResponse getRepaymentAbility(@RequestBody PerformanceRankingYmQO performanceRankingYmQO) {
        try {
            RepaymentAbility repaymentAbility = customerContractDbService.getRepaymentAbility(performanceRankingYmQO.getYmDateStr(), performanceRankingYmQO.getAccountId());
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), JSONObject.toJSON(repaymentAbility));
        } catch (Exception e) {
            log.error("performanceRanking companyPerformanceRanking error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }


}
